﻿/* 
	PureMVC CSharp / WPF / EmployeeAdmin Demo - Login
	By Andy Adamczak <andy.adamczak@puremvc.org>
	Copyright(c) 2009 Andy Adamczak, Some rights reserved.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PureMVC.Patterns;
using PureMVC.Interfaces;

using GEF2.Model;
//using GEF2.View.Components;
using GEF2.Model.Storage;
using GEF2;

namespace GEF2.View
{
	public class EntityFormMediator : Mediator, IMediator
	{
		private EntityProxy entityProxy;

		public new const string NAME = "EntityFormMediator";

        public EntityFormMediator(MainWindow viewComponent)
			: base(NAME, viewComponent)
		{
            MainWindow.AddEntity += new EventHandler(MainWindow_AddEntity);
            MainWindow.DeleteEntity += new EventHandler(MainWindow_DeleteEntity);
        }

		public override void OnRegister()
		{
			base.OnRegister();
            entityProxy = (EntityProxy)Facade.RetrieveProxy(EntityProxy.NAME);
		}

        private MainWindow MainWindow
		{
            get { return (MainWindow)ViewComponent; }
		}

        void MainWindow_AddEntity(object sender, EventArgs e)
        {
            if (e is LocationEventArgs)
            {
                SendNotification(ApplicationFacade.ADD_ENTITY, ((LocationEventArgs)e).Location);
            }
        }

        void MainWindow_DeleteEntity(object sender, EventArgs e)
        {
            if (e is DeleteEntityEventArgs)
            {
                SendNotification(ApplicationFacade.DELETE_ENTITY, ((DeleteEntityEventArgs)e).Entity);
            }
        }

		public override IList<string> ListNotificationInterests()
		{
			IList<string> list = new List<string>();
            list.Add(ApplicationFacade.ENTITY_ADDED);
            list.Add(ApplicationFacade.ENTITY_DELETED);
            return list;
		}

		public override void HandleNotification(INotification note)
		{
			Entity entity;

			switch (note.Name)
			{
                case ApplicationFacade.ENTITY_ADDED:
                    entity = (Entity)note.Body;
                    MainWindow.DrawEntity(entity);
                    break;
                case ApplicationFacade.ENTITY_DELETED:
                    MainWindow.Invalidate();
                    break;
			}
		}
	}
}
